package com.simlink.sns.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.simlink.sns.model.Event;
import com.simlink.sns.model.Tag;
import com.simlink.sns.model.User;
import com.simlink.sns.service.EventService;
import com.simlink.sns.service.FeedService;
import com.simlink.sns.service.FollowService;
import com.simlink.sns.service.InterestService;
import com.simlink.sns.service.TagService;
import com.simlink.sns.service.UserService;
import com.simlink.sns.util.Dic;
import com.simlink.sns.util.ShiroUtils;

@Controller
public class HomePage {

	@Autowired
	@Qualifier("eventService")
	private EventService eventService;

	@Autowired
	@Qualifier("feedService")
	private FeedService feedService;

	@Autowired
	@Qualifier("userService")
	private UserService userService;

	@Autowired
	@Qualifier("followService")
	private FollowService followService;

	@Autowired
	@Qualifier("tagService")
	private TagService tagService;

	@Autowired
	@Qualifier("interestService")
	private InterestService interestService;

	
	public ModelAndView showHomePage() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("index");
		
		Object user = SecurityUtils.getSubject().getPrincipal();
		if (user == null) {
			return mav;
		}
		User u = (User)user;
		mav.addObject("counter", userService.getCounterOfFollowAndShortPost(ShiroUtils.getUserId()));
		List<Event> feeds = feedService.getFeeds(ShiroUtils.getUserId());
		mav.addObject("feeds", feeds);

		mav.addObject("dic", new Dic());
		return mav;

	}
	
	@GetMapping("/feeds")
	public List<Event> getFeeds() {
		return feedService.getFeeds(ShiroUtils.getUserId());
	}

	@RequestMapping("/popup_usercard/{user_id}")
	public ModelAndView getPopupUserCard(@PathVariable("user_id") String user_id) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("popup_usercard");
		User user = userService.findById(Integer.valueOf(user_id));
		if (user != null) {
			mav.addObject("u", user);
			mav.addObject("counter", userService.getCounterOfFollowAndShortPost(Integer.valueOf(user_id)));
		}

		return mav;
	}

	@RequestMapping("/page/{num}")
	public ModelAndView nextPage(@PathVariable("num") String num_str) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("nextpage");

		int num = Integer.parseInt(num_str);
		List<Event> feeds = feedService.getFeedsOfPage(ShiroUtils.getUserId(), num);
		mav.addObject("feeds", feeds);
		mav.addObject("dic", new Dic());
		return mav;
	}

	@RequestMapping("/welcome")
	public ModelAndView welcome() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("welcome");
		List<Tag> tags_recommend = tagService.getRecommendTags(0);
		mav.addObject("tags", tags_recommend);
		mav.addObject("dic", new Dic());
		return mav;
	}

	@RequestMapping("/sidebar")
	public ModelAndView sideBar() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("sidebar");
		User user = ShiroUtils.getUser();
		if (user == null) {
			return mav;
		}

		List<User> rec_users = userService.getRecommendUsers(user == null ? 0 : user.getId(), 4);
		mav.addObject("isFollowings", followService.isFollowing(user == null ? 0 : user.getId(), rec_users));
		mav.addObject("popusers", rec_users);

		List<Tag> tags_recommend = tagService.getRecommendTags(user == null ? 0 : user.getId());
		mav.addObject("poptags", tags_recommend);

		return mav;
	}

	@RequestMapping("/guide")
	public ModelAndView guide() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("guide");

		User user = ShiroUtils.getUser();
		List<Tag> tags_recommend = tagService.getRecommendTags(user == null ? 0 : user.getId());
		mav.addObject("tags", tags_recommend);
		mav.addObject("isInterests",
				interestService.hasInterestInTags(user == null ? 0 : user.getId(), tags_recommend));

		mav.addObject("dic", new Dic());
		return mav;
	}

	/**
	 * 新用户兴趣选择之后 feed初始化
	 * 
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/guide/ok")
	public Map<String, Object> guideOk() {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = ShiroUtils.getUser();
		feedService.codeStart(user.getId());

		return map;
	}

	@RequestMapping("/followers")
	public ModelAndView getFollowers() {
		ModelAndView mav = new ModelAndView();
		mav.addObject("followers", userService.findAllbyIDs(followService.getFollowerIDs(ShiroUtils.getUserId())));

		mav.setViewName("follower");
		return mav;
	}

	@RequestMapping("/followings")
	public ModelAndView getFollowings() {
		ModelAndView mav = new ModelAndView();
		mav.addObject("followings", userService.findAllbyIDs(followService.getFollowingIDs((ShiroUtils.getUserId()))));

		mav.setViewName("following");
		return mav;
	}

	@RequestMapping("/404")
	public ModelAndView pageNotFound() {
		return new ModelAndView("404");
	}

	@RequestMapping("/500")
	public ModelAndView error500() {
		return new ModelAndView("500");
	}
}
